<?php

namespace App\Email;

class Parse
{
    public $data = array();

    public function __construct()
    {

    }

    public function parse($file)
    {
        $this->data = array();
        $parser = new \ezcMailParser();
        $mail = $parser->parseMailStr( $file );
        $this->formatMail( $mail[0] );
        return $this->data;
    }

    public function formatMail( $mail )
    {
        $this->data['from'] = $this->formatAddress( $mail->from );
        $this->data['to'] =  $this->formatAddresses( $mail->to );
        $this->data['cc'] = $this->formatAddresses( $mail->cc );
        $this->data['bcc'] = $this->formatAddresses( $mail->bcc );
        $this->data['date'] = date('Y-m-d H:i:s', $mail->timestamp );
//        $this->data['date'] = $mail->timestamp;
        $this->data['subject'] = $mail->subject;
        $this->data['messageid'] = $mail->messageId;
        $this->formatMailPart( $mail->body );
        return $this->data;
    }
    public function formatMailPart( $part )
    {
//        echo '<br/>';
//        echo get_class($part);
        if ( get_class($part) == 'ezcMail' )
            return $this->formatMail( $part );
        if ( get_class($part) == 'ezcMailText' )
            return $this->formatMailText( $part );
        if ( get_class($part) == 'ezcMailFile' )
            return $this->formatMailFile( $part );
        if ( get_class($part) == 'ezcMailRfc822Digest' )
            return $this->formatMailRfc822Digest( $part );
        if ( get_class($part) == 'ezcMailMultiPart' )
            return $this->formatMailMultipart( $part );
        return $this->formatMailMultipart( $part );
    }
    public function formatMailMultipart( $part )
    {
//        echo '<br/>';
//        echo get_class($part);
        if ( get_class($part) == 'ezcMailMultipartAlternative' )
            return $this->formatMailMultipartAlternative( $part );
        if ( get_class($part) == 'ezcMailMultiPartDigest' )
            return $this->formatMailMultipartDigest( $part );
        if ( get_class($part) == 'ezcMailMultiPartRelated' )
            return $this->formatMailMultipartRelated( $part );
        if ( get_class($part) == 'ezcMailMultipartMixed' )
            return $this->formatMailMultipartMixed( $part );
        die( "No clue about the ". get_class( $part ) . "\n" );
    }
    public function formatMailMultipartMixed( $part )
    {
        foreach ( $part->getParts() as $key => $alternativePart )
        {
            $data[] = $this->formatMailPart( $alternativePart );
        }
        return $data;
    }
    public function formatMailMultipartRelated( $part )
    {
        $this->data['add'][] = $this->formatMailPart( $part->getMainPart() );
        foreach ( $part->getRelatedParts() as $key => $alternativePart )
        {
            $data =  $this->formatMailPart( $alternativePart );
        }
//        $this->data['add'][] = $data;
        return $data;
    }
    public function formatMailMultipartDigest( $part )
    {
        foreach ( $part->getParts() as $key => $alternativePart )
        {
            $data = $this->formatMailPart( $alternativePart );
        }
//        $this->data['add'][] = $data;
        return $data;
    }
    public function formatMailRfc822Digest( $part )
    {
        $this->data['digest'] = $this->formatMailpart( $part->mail );
        return $this->data['digest'];
    }
    public function formatMailMultipartAlternative( $part )
    {
        foreach ( $part->getParts() as $key => $alternativePart )
        {
            $this->formatMailPart( $alternativePart );
        }
        return $this->data['alternative'];
    }
    public function formatMailText( $part )
    {
        $this->data['text'] .= $part->text;
        return $this->data['text'];
    }
    public function formatMailFile( $part )
    {
        $data = array();
//        $t .= "Disposition Type: {$part->dispositionType}\n";
//        $t .= "Content Type:     {$part->contentType}\n";
//        $t .= "Mime Type:        {$part->mimeType}\n";
//        $t .= "Content ID:       {$part->contentId}\n";
        $data['name'] =   $part->contentDisposition->displayFileName;
        $data['fileName'] =   $part->fileName;
        $this->data['add'][] = $data;
        return $data;
    }
    public function formatAddresses( $addresses )
    {
        $fa = array();
        foreach ( $addresses as $address )
        {
            $fa[] = $this->formatAddress( $address );
        }
        return implode( ', ', $fa );
    }
    public function formatAddress( $address )
    {
        $name = '';
        if ( !empty( $address->name ) )
        {
            $name = "{$address->name} ";
        }
        return $name . "<{$address->email}>";
    }
}
?>